package gui;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import constants.Cmd;

public class AddRecordFrame extends JFrame implements ActionListener{
	private JButton ok, cancel;
	private JPanel textFields, buttons, labels, centerPanel;
	private JTextField patientNamn,nurse,hospital;
	private JTextArea medicalData;
	private JLabel patientNamnL,nurseL,hospitalL,medicalDataL;
	private GUIControl gc;
	public AddRecordFrame(GUIControl gc){
		this.gc=gc;
		this.setTitle("Add New Record");
		this.setLayout(new BorderLayout());
		centerPanel=new JPanel();
		textFields=new JPanel(new GridLayout(3,1));
		buttons=new JPanel();
		labels=new JPanel(new GridLayout(12,1));
		this.add(centerPanel, BorderLayout.CENTER);
		centerPanel.add(textFields);
		this.add(buttons, BorderLayout.PAGE_END);
		this.add(labels, BorderLayout.WEST);
		ok=new JButton("Ok");
		cancel=new JButton("Cancel");
		buttons.add(ok);
		buttons.add(cancel);
		ok.addActionListener(this);
		cancel.addActionListener(this);
		initTextFields();
		initTextArea();
		initLabels();
		this.pack();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	private void initTextArea() {
		medicalData=new JTextArea();
		medicalData.setWrapStyleWord(true);
		JScrollPane sp = new JScrollPane(medicalData);
		medicalData.setLineWrap(true);
		medicalData.setRows(10);
		sp.setPreferredSize(new Dimension(250, 250));
		centerPanel.add(sp);
		sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		centerPanel.setPreferredSize(new Dimension(250,400));
		
		
	}
	private void initLabels() {
		patientNamnL=new JLabel("Patient name: ");
		nurseL=new JLabel("Nurse: ");
		hospitalL=new JLabel("Hospital: ");
		medicalDataL=new JLabel("Medical data: ");
		labels.add(patientNamnL);
		labels.add(nurseL);
		labels.add(hospitalL);
		labels.add(medicalDataL);
	}
	private void initTextFields() {
		patientNamn=new JTextField();
		nurse=new JTextField();
		hospital=new JTextField();
		
		textFields.add(patientNamn);
		textFields.add(nurse);
		textFields.add(hospital);
		textFields.setPreferredSize(new Dimension(250,100));
	}
	public void clear(){
		patientNamn.setText("");
		nurse.setText("");
		hospital.setText("");
		medicalData.setText("");
	}
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand()=="Ok"){
			gc.sendToServer(Cmd.newRecord+patientNamn.getText()+"#"+nurse.getText()+"#"+hospital.getText()+"#"+medicalData.getText());
			this.setVisible(false);
			this.clear();
		}else if(arg0.getActionCommand()=="Cancel"){
			this.setVisible(false);
			this.clear();
			System.exit(0);
		}
		
	}
}
